package com.ruoyi.web.controller.api;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.framework.web.service.SysLoginService;
import com.ruoyi.web.controller.api.vo.LoginReq;
import com.ruoyi.web.controller.api.vo.SysUserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author yejingang
 * @since 2024-08-08
 */
@Api("用户相关接口")
@RestController
@RequestMapping("/api")
public class UserController extends BaseController {

    @Autowired
    private SysLoginService loginService;

    @ApiOperation("登录")
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginReq loginBody) {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid());
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    @ApiOperation("获取用户列表")
    @GetMapping("getInfo")
    public R<SysUserVO> getInfo() {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        if (user != null && StringUtils.isBlank(user.getAvatar())) {
            user.setAvatar("/avatar/default.jpeg");
        }
        SysUserVO userVO = new SysUserVO();
        BeanUtils.copyBeanProp(userVO, user);
        return R.ok(userVO);
    }

}
